package com.example.apiproject.service.impl;

import com.example.apiproject.dao.ReportDao;
import com.example.apiproject.entity.Report;
import com.example.apiproject.jwt.jwtutil;
import com.example.apiproject.service.ReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDao reportDao;
    @Autowired
    private jwtutil jwtUtils;
    @Override
    public int insert(HttpServletRequest request, String articleId, String types, String content) {
        String token = request.getHeader("token");
        if(StringUtils.isBlank(token)){
            token = request.getParameter("token");
        }
        int  userId = jwtUtils.getUserId(token);
        return   reportDao.insert(userId, articleId, types, content, LocalDateTime.now());
    }

    @Override
    public int update(Report report) {
        return reportDao.update(report);
    }

    @Override
    public List<Report> queryByStatus(Integer status) {
        return reportDao.queryByStatus(status);
    }

    @Override
    public int delete(Integer id) {

        return reportDao.delete(id);
    }

    @Override
    public List<Report> queryByUserId(HttpServletRequest request) {
        String token = request.getHeader("token");
        if(StringUtils.isBlank(token)){
            token = request.getParameter("token");
        }
        int  userId = jwtUtils.getUserId(token);
        return reportDao.queryByUserId(userId);
    }

    @Override
    public List<Report> queryAll() {
       return reportDao.querAll();
    }
}
